/**
 * @author mc
 * @date 2021/7/20 2:52 下午
 */
(function (window) {
    let listener = () => {}
    let observer = null
    let selectorStr = null
    const ready = (selector, callback) => {
        listener = callback
        selectorStr = selector
        if (!observer) {
            observer = new MutationObserver(check)
            observer.observe(window.document.documentElement, {
                childList: true,
                subtree: true
            })
        }
    }
    const check = () => {
        // 因为执行check函数异步的，所以当执行时 selectorStr 和 callback 生效的是用户最后定义的
        // 因此这里是有bug的
        const elements = document.querySelectorAll(selectorStr) || []
        elements.forEach(element => {
            if (!element.ready) {
                element.ready = true
                listener.call(element, element)
            }
        })
    }
    window.ready = ready
})(window)
